package com.ciei.dpagm.util;

import org.springframework.web.multipart.MultipartFile;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

/**
 * @author liuqi
 */
public class Base64DecodedMultipartFile implements MultipartFile {
        private final byte[] bytes;
        private final String name;
        private final String originalFileName;
        private final String contentType;

        public Base64DecodedMultipartFile(byte[] bytes, String filename) {
            this.bytes = bytes;
            this.name = "file";
            this.originalFileName = filename;
            // 这里需要根据实际情况设置文件类型
            this.contentType = "application/octet-stream";
        }

        @Override
        public String getName() {
            return name;
        }

        @Override
        public String getOriginalFilename() {
            return originalFileName;
        }

        @Override
        public String getContentType() {
            return contentType;
        }

        @Override
        public boolean isEmpty() {
            return bytes == null || bytes.length == 0;
        }

        @Override
        public long getSize() {
            return bytes.length;
        }

        @Override
        public byte[] getBytes() throws IOException {
            return bytes;
        }

        @Override
        public InputStream getInputStream() throws IOException {
            return new ByteArrayInputStream(bytes);
        }

        @Override
        public void transferTo(File file) throws IOException, IllegalStateException {
            new FileOutputStream(file).write(bytes);
        }
    }
